define([
        //3.加载依赖的库
        'jquery',
        'knockout',
        'text!./asset.html',
        'bootstrap',
   //     'uui',
        'director',
    //    'grid',
   //     'dragJW',
        'domReady!',
        'datatables.net-bs',
        // 'datatables.net-buttons',
        'datatables.net-autofill-bs',
        'net-jszip',
        'net-pdfmake',
        'datatables.net-buttons-bs',
        'datatables.net-colreorder',
        'datatables.net-rowreorder',
        'datatables.net-select',
        'datatables.net-scroller',
        'datatables.net-fixedHeader',
        // 'datatables.net-editor',
        'datatables.net-keyTable',
        'datatables.net-editor-bs',
        'datatables.net-responsive',
        'jstree',
    ],
	function($, ko, template) {
		var viewModelAssetType = {
			data : ko.observable({}),
			assetTypeDataTable: new u.DataTable({
                meta: {
                	'id': {},
                    'nd': {}, 
                    'assetTypeCode': {}, 
                    'assetTypeName': {}, 
                    'pcode': {}  
                }
            }),
		};
		
		viewModelAssetType.getAllAssetType = function(){
            app.serverEvent().addDataTable("assetTypeDataTable", "all").fire({
                url: $ctx + '/evt/dispatch',
                ctrl: 'baseData.assetType.assetTypeController',
                method: 'loadAssetType',
                success: function (data) {
                	var selectid = "#assettype-search-type";
                	viewModelAssetType.assetTypeSelect(selectid);
                	var selectid = "#assettype-edit-nd";
                	viewModelAssetType.assetTypeSelect(selectid);
                }
            })
        }
		
		
		viewModelAssetType.assetTypeSelect = function (selectid ) {
//            var selectid = "#assettype-search-type";
            $.ajax({
                url: $ctx + "/assetType/getAllNd",
                type: "GET",
                dataType: "json",
                data: {},
                success: function (data) {
                    $(selectid).css("color", "");
                    $(selectid).html("");
                    var please_choice = '<option>请选择会计年度</option>';
                    $(selectid).append(please_choice);
                    for (var i = 0; i < data.length; i++) {
                        var temp = '<option>' + data[i] + '</option>';
                        $(selectid).append(temp);
                    }
                }
            });
        }
		
		
		
		viewModelAssetType.searchAssetType = function() {
            var assetType_nd = $("#assettype-search-type").val();
            if(assetType_nd == "请选择会计年度"){
            	assetType_nd = "";
            }
            var queryData = {};
            queryData["search_EQ_nd"] = assetType_nd;
            // queryData["search_LIKE_dicName"] = dic_name;
            viewModelAssetType.assetTypeDataTable.addParams(queryData);
            app.serverEvent().addDataTable("assetTypeDataTable", "all").fire({
                url: $ctx + '/evt/dispatch',
                ctrl: 'baseData.assetType.assetTypeController',
                method: 'loadAssetType',
                success: function (data) {}
            })

        }
		
        assetTypeSetFun = function(obj) {
            obj.element.innerHTML = '<span id="'+ obj.rowIndex +'" onclick="addAssetType(this.id)" class="assetType-fun">新增</span><span class="separator">|</span><span id="'+ obj.rowIndex +'" onclick="editAssetType(this.id)" class="assetType-fun">编辑</span><span class="separator">|</span><span id="'+ obj.rowIndex +'" onclick="delAssetType(this.id)" class="assetType-fun">删除</span>';
        }		
		
        delAssetType = function() {
//            var index = viewModelAssetType.assetTypeDataTable.getCurrentRow().rowId;        	
//            $("#jump-delete-text").attr("class",index);
            $("#delete-assettype-notice").modal({backdrop: 'static', keyboard: false});
        } 
        
        viewModelAssetType.delete_assettype_close = function() {
            $("#delete-assettype-notice").modal("hide");
        }        
        viewModelAssetType.delete_assettype = function() {
            var data = viewModelAssetType.assetTypeDataTable.getCurrentRow().data;
//            document.getElementById('modal-delete-text').innerHTML = "确认要删除【" + delHint + "】吗？";
//            var index = $("#jump-delete-text").attr("class");
//            var selected_node = $('#assettype-grid').parent()[0]['u-meta'].grid.getRowByIndex(index);
            var id = data.id.value;
            $.ajax({
                url: $ctx + "/assetType/delAssetType",
                type: "POST",
                dataType: "json",
                data: {
                	id: id,
                },
                success: function (data) {
                    if(data.result == "success"){
                        $("#delete-assettype-notice").modal("hide");
                        viewModelAssetType.getAllAssetType();
                        //3秒消失
                        $("#save-success-new").css("display","block");
                        $("#save-success-new").fadeOut(3000);
                    } else {
                        $("#delete-assettype-notice").modal("hide");
                        $("#error-text").text(data.reason);
                        $("#error-notice").modal({backdrop: 'static', keyboard: false});
                    }
                }
            });
        }   
        
        
		editAssetType = function() {
//            $("#assettype-edit-notice").modal({backdrop: 'static', keyboard: false});
//			var data = $('#assettype-grid').parent()[0]['u-meta'].grid.getCurrentRow().data;
			var data = viewModelAssetType.assetTypeDataTable.getCurrentRow().data;
            var id = data.id.value;
            var nd = data.nd.value;
            var assetTypeCode = data.assetTypeCode.value;
            $("#assettype-edit-id").val(data.nd.value);
            $("#assettype-edit-nd").val(data.nd.value);
            $("#assettype-edit-code").val(data.assetTypeCode.value);
            $("#assettype-edit-name").val(data.assetTypeName.value);
            $("#assettype-edit-pcode").val(data.pCode.value);
//            $("#jump-delete-text").attr("class",index);
			$("#assettype-edit-nd").attr("disabled","disabled");
			$("#assettype-edit-code").attr("disabled","disabled");
            $("#assettype-edit-notice").modal({backdrop: 'static', keyboard: false});
        }
		
		
		addAssetType = function() {
//			var index = viewModelBookType.bookTypeDataTable.getCurrentRow().rowId;
//            $("#jump-delete-text").attr("class",index);
            $("#assettype-edit-notice").modal({backdrop: 'static', keyboard: false});
        }
		        
        		
        
        function showWarnInfo(checkResult,checkinfo){
        	if (!checkResult){
                $("#warn-text").text(checkinfo);
                if($('#warn-text').css("display")=="none")
                {
                	$("#warn-text").css("display","inline-block");
                } 
            } else {
            	$("#warn-text").css("display","none");
            }
        }
		viewModelAssetType.checkNd = function () {
            var nd = $("#assettype-edit-nd").val();
            //注意正则表达式中的\为转义字符，需要用\\来处理 
            var patt=new RegExp("^\\d{4}$");
            var checkResult= patt.test(nd);
            showWarnInfo(checkResult,"请输入正确年度");
            return checkResult;
        }
        
		viewModelAssetType.checkAssetTypeCode = function () {
            var code = $("#assettype-edit-code").val();
            var patt = new RegExp("^\\d{1,9}$");
            var checkResult= patt.test(code);
            showWarnInfo(checkResult,"请输入正确科目代码：只能为数字");
            return checkResult;
        }		
		
        checkInput = function () {
        	return viewModelAssetType.checkAssetTypeCode();
        }		
		
		
		viewModelAssetType.saveAssetTypeEdit = function() {
        	if (!checkInput()){
        		return;
        	}
            var assetType_Id = $("#assettype-edit-id").val();
            var assetType_Nd = $("#assettype-edit-nd").val();
            var assetType_Code = $("#assettype-edit-code").val();
            var assetType_Name = $("#assettype-edit-name").val();
            var assetType_Pcode = $("#assettype-edit-pcode").val();

            $.ajax({
                url: $ctx + "/assetType/saveAssetType",
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                	id: assetType_Id,
                    nd: assetType_Nd,
                    assetTypeCode: assetType_Code,
                    assetTypeName: assetType_Name,
                    pcode: assetType_Pcode,
                }),
                success: function (data) {
                	viewModelAssetType.sendAjaxSuccess(data);
                }
            });
        }
        

        
		viewModelAssetType.sendAjaxSuccess = function(data) {
             if(data.result == "success"){
                 $("#save-success-new").css("display","block");
                 $("#save-success-new").fadeOut(3000);
                 viewModelAssetType.getAllAssetType();
                 $("#assettype-list-tab").click();
                 viewModelAssetType.cancelAssetTypeEdit();
             } else {
                 if(data.detailKey != undefined){
                     $("#nd-error-repeat").text(data.detailKey);
                     $("#nd-error-repeat").css("display","inline-block");
                 } else {
                     $("#nd-error-repeat").css("display","none");
                 }
                 if(data.dicType != undefined) {
                     $("#type-error-repeat").text(data.dicType);
                     $("#type-error-repeat").css("display","inline-block");
                 } else {
                     $("#type-error-repeat").css("display","none");
                 }
             }
         }
		viewModelAssetType.cancelAssetTypeEdit = function() {
            $("#assettype-list-tab").click();
            $("#assettype-edit-id").val("");
            $("#assettype-edit-nd").val("");
            $("#assettype-edit-code").val("");
            $("#assettype-edit-name").val("");
            $("#assettype-edit-pcode").val("");
            $("#nd-error").css("display","none");
            $("#code-error").css("display","none");
            $("#name-error").css("display","none");
            $("#pcode-error").css("display","none");
            $("#assettype-edit-nd").removeAttr("disabled");
            $("#assettype-edit-code").removeAttr("disabled");
            $("#edit-notice").modal("hide");
            $("#assettype-edit-notice").modal("hide");

        }
        
		var init = function(){
			ko.cleanNode($('.content')[0]);
            app = u.createApp(
                {
                    el:'.content',
                    model: viewModelAssetType
                }
            );
         //   viewModelAssetType.getAllAssetType();
            var table = $('#assettype-grid').DataTable({
                language: {
                	"info": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                	 "paginate": {
                         "first": "首页",
                         "previous": "前一页",
                         "next": "后一页",
                         "last": "尾页"
                     },
                    //"url": "chinese.lang.json"
                    select: {
                        rows: {
                            _: '%d 行被选择',
                            0: '',
                            1: '1行被选择'
                        }
                    }
                },
                colReorder: true,
                //rowReorder: true,
                lengthChange: false,
                searching:false,
                // "bScrollCollapse": false,
                //      pagingType: 'full_numbers',
                paging: true,
                serverSide: true,
//                "dom": '<"wrapper"flipt>',
                ajax: {
                    url: "/agcfs/baseData/assetType/getAssetList",
                    dataType: "json",
                },
                scrollX: true,
                scrollY: true,
                // scrollY:        200,
                //deferRender:    true,
                // scroller:       true,
//                fixedColumns: {        /*1.5.4 有bug,错位*/
//                    leftColumns: 2
//                },
//                 responsive: true,
                rowid: "id",
                columns: [
                    {   // Checkbox select column
                        data: null,
                        defaultContent: '',
                        className: 'select-checkbox',
                        orderable: false
                    },
                    {data: "nd"},
                    {data: "assetTypeCode"},
                    {data: "assetTypeName"},
                    {data: "pcode"},
                ],
                order: [ 1, 'asc' ], //通过第一列排序 然后重绘表格并维持当前分页的信息
                
                select: {
                    style: 'multi', /* os,single,multi, multi+shift,api*/
                    selector: 'td.select-checkbox',
                }
            });
		};
		
		return {
			'model' : viewModelAssetType,
			'template' : template,
			'init' : init
		};
	}
);
